<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
</head>
<body>
<div id="app">
    <el-page-header
            style="background-color: #0096dc;color: white;
                    line-height: 60px"
            @back="goBack" content="修改轮播图">
    </el-page-header>
    <!--显示原来的图片-->
    <img :src="oldUrl" width="200" alt="">
    <el-upload
            action="/upload"
            name="picFile"
            limit="1"
            list-type="picture-card"
            :on-success="handleSuccess"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove">
        <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    <el-button type="success" @click="update()">修改轮播图</el-button>
</div>
</body>
<!--引入axios和vue框架-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
                banner:{
                    url:""
                },
                oldUrl:""
            }
        },
        created:function () {
          //发请求获取需要修改的轮播图url
          axios.get("/banner/selectById"+location.search)
              .then(function (response) {
              v.oldUrl = response.data;
          })
        },
        methods:{
            goBack(){
                //返回上一页面
                history.back();
            },
            handleRemove(file, fileList) {
                console.log(file, fileList);
                v.banner.url="";
            },
            handlePictureCardPreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            update(){
                if (v.banner.url==""){
                    v.$message.error("请选择上传的图片!");
                    return;
                }
                //得到地址栏中的id   "?id=5"    拆分后["?id","5"]
                let id = location.search.split("=")[1];
                //把id装进轮播图对象里面
                v.banner.id = id;
                //发出异步请求
                axios.post("/banner/update",v.banner).then(function () {
                    //添加完成后返回后台管理页面
                    location.href="/admin.html";
                })
            },
            handleSuccess(response,file,fileList){
                //response代表图片名
                v.banner.url=response;
            }
        }
    })
</script>
</html>